package com.lzw.breadknife.sample.mvvm.base;

import com.lzw.breadknife.Unbinder;

import java.lang.ref.WeakReference;

/**
 * author  : AWeiLoveAndroid
 * e-mail  : lzw460433675@163.com
 * github  : https://gitee.com/AWeiLoveAndroid/BreadKnife
 * project : BreadKnife
 */
public abstract class AbsViewModel<T> implements IBaseViewModel{
    protected WeakReference<T> mViewRef;
    protected WeakReference<Unbinder> mBinderRef;

    protected void attachView(T view) {
        mViewRef = new WeakReference<T>(view);
    }

    protected void attachUnbinder(Unbinder unbinder) {
        mBinderRef = new WeakReference<Unbinder>(unbinder);
    }

    protected T getView() {
        return mViewRef.get();
    }

    protected Unbinder getUnbinder() {
        return mBinderRef.get();
    }

    public boolean isViewAttached() {
        return mViewRef != null && mViewRef.get() != null & mBinderRef != null & mBinderRef.get() != null;
    }

    public void dettachView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
        if (mBinderRef != null) {
            mBinderRef.clear();
            mBinderRef = null;
        }
    }
}
